package com.fuxue.mapper;

import com.fuxue.dto.*;
import com.fuxue.entity.Answer;
import com.fuxue.entity.Image;
import com.fuxue.entity.Question;
import com.fuxue.entity.WriteSelect;
import com.fuxue.vo.*;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface QuestionMapper {


    List<QuestionAndColumVO> getQuestionById(Integer id);

    Page<QuestionVO> pageQury(QuestionPageShowDTO questionPageShowDTO);
     @Select("select * from question where q_id = #{id}")
     Question getQuestionALL(Integer id);

    @Select("select url from image where aq_id = #{id} and type = #{i} order by creat_time desc")
    List<String> getQuestionImage(Integer id, int i);

    @Insert("insert into answer (q_id, server_id, server_name, server_avatar, context, creat_time) "+"values"
    +"(#{qd},#{serverId},#{serverName},#{serverAvatar},#{context},#{creatTime})")
    void setQuestion(WriteQuestionDTO writeQuestionDTO);

    @Insert("insert into image (aq_id,type,url,creat_time)"+"values"+
    "(#{aqId},#{type},#{url},#{creatTime})")
    void setImage(Image image);

    @Select("select answer_id from answer where q_id = #{qd}")
    Integer getAnswerId(Integer qd);

    @Update("update server set point = point + 5 where server_id = #{serverId}")
    void addPoint(Integer serverId);

    @Update("update question set is_finished = #{id} where q_id = #{qd}")
    void updateFinished(int qd,int id);

    @Insert("INSERT INTO question (user_id, user_name, user_avatar, title, context, type, is_publiced, creat_time) " +
            "VALUES ( #{userId}, #{userName}, #{userAvatar}, #{title}, #{context}, #{type}, #{isPubliced}, #{creatTime})")
    void insertQuestion(WriteDTO writeDTO);

    @Select("select distinct filed_name from filed")
    FiledVO FindF();


    @Insert("insert into image (aq_id, type, url, creat_time) VALUES (#{aqId},#{type},#{url},#{creatTime})")
    void insertWriteImage(Image image);

    List<QuestionUserFirsetVO> getUserQuestionById(Integer id);

    List<QuestionUserFirsetVO> likeSelect(QuestonTorCDTO questonTorCDTO);

    Page<QuestionVO> UserpageQury(UserQuestionPageShowDTO userQuestionPageShowDTO);
    @Select("select answer_id, q_id, server_id, server_name, " +
            "server_avatar, like_count, commit_count, context, creat_time, title, type, " +
            "if_see from answer where q_id = #{id} ")
    Answer getAnswerALL(Integer id);

    @Select("select url from image where aq_id = #{id} and type = #{i} order by creat_time desc")
    List<String> getAnswerImage(Integer id, int i);
    @Select("select q_id from question where user_id = #{userId} order by creat_time desc LIMIT 1")
    Integer getQId(Integer userId);

    @Select("select answer_id,server_name,server_avatar,context,creat_time from answer " +
            "where answer_id = #{aId}")
    AnswerDetialsVO getAnswerDetials(int aId);

    WriteSelect getIdName();
    @Update("UPDATE question set server_id = #{serverId},server_name = #{serverName},is_finished = 0 where q_id = #{qId}")
    void updateServerFinished(WriteSelect writeSelects);
}
